package tt.dz.appserver.dao.ext;

import java.util.List;
import java.util.Map;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Repository;

import tt.dz.appserver.dao.base.BasisDao;
import tt.dz.appserver.entity.model.StationComment;
import tt.dz.appserver.util.Constants;


/**
 * 站点评论dao
 * @author Administrator
 *
 */
@Repository
public class StationCommentDao extends BasisDao<StationComment>{

	private static final String FIND_STATION_COMMENT_GRADE = "select IFNULL((select(sum(c.comment_grade)/count(*)) as grade from as_station_comment c where c.station_id=?),4)  as grade";
	
	/**
	 * 根据站点ID查询评论
	 * @param stationId
	 * @param pageNo
	 * @return
	 */
	@Caching(cacheable={
			@Cacheable(value="stationComment",key="#stationId+#pageNo"),
	})
	public List<StationComment> getStationCommentByStationId(String stationId, Integer pageNo){
		
		StationComment stationComment = new StationComment();
		stationComment.setStation_id(stationId);//站点ID
		return super.findExample(stationComment, pageNo, Constants.PAGE_SIZE, "comment_time", 0);
	}
	
	
	/**
	 * 保存评论
	 */
	@Caching(evict={
			@CacheEvict(value="stationComment", allEntries=true),
			@CacheEvict(value="stationGrade", key="#stationComment.station_id+'grade'")
	})
	@Override
	public int save(StationComment stationComment) {
		return super.save(stationComment);
	}
	
	/**
	 * 查询站点评论星级
	 * @param stationId
	 * @return
	 */
	@Cacheable(value="stationGrade", key="#stationId+'grade'")
	public String findStationCommentGrade(String stationId){
		
		List<Map<String,Object>> gradeList = super.findDataOfMapBySQL(new StringBuffer(FIND_STATION_COMMENT_GRADE), null, null, stationId);
		if(gradeList.size()>0){
			return gradeList.get(0).get("grade").toString();
		}
		return "5";
	}
	
}
